﻿/*
 * Programer: JR Padfield
 * Description: Handles basic game screen functions.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System;
using Microsoft.Xna.Framework;
using TheWorldLibrary.GameStates;
using TheWorldLibrary.Controls;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using TheWorld.Networking;
using TheWorld.Global;

namespace TheWorld.GameScreens
{
    public abstract partial class BaseGameState : GameState
    {
        #region Fields region

        public static Client GameRef;

        protected ControlManager ControlManager;

        private PlayerIndex PlayerIndexInControl;

        private BaseGameState _transitionTo;

        private bool _transitioning;

        private ChangeType _changeType;

        private TimeSpan _transitionTimer;
        private readonly TimeSpan _transitionInterval = TimeSpan.FromSeconds(0.5);

        GfxManager GfxM = GfxManager.Instance;

        #endregion

        #region Properties region
        #endregion

        #region Constructor Region

        public BaseGameState(Game game, GameStateManager manager)
            : base(game, manager)
        {
            GameRef = (Client)game;

            PlayerIndexInControl = PlayerIndex.One;
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            ContentManager Content = Game.Content;

            var menuFont = Content.Load<SpriteFont>(@"Fonts\ControlFont");
            ControlManager = new ControlManager(menuFont);

            //Mapping.Instance.Worlds = new World(GameRef, GameRef.ScreenRectangle);

            //Mapping.Instance.LoadMaps();            

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (_transitioning)
            {
                _transitionTimer += gameTime.ElapsedGameTime;

                if (_transitionTimer >= _transitionInterval)
                {
                    _transitioning = false;
                    switch (_changeType)
                    {
                        case ChangeType.Change:
                            StateManager.ChangeState(_transitionTo);
                            break;
                        case ChangeType.Pop:
                            StateManager.PopState();
                            break;
                        case ChangeType.Push:
                            StateManager.PushState(_transitionTo);
                            break;
                    }
                }
            }

            Network.Instance.RecieveData();

            if (Static.menuAlertTimer < (int)Static._gameTime.ElapsedGameTime.TotalMilliseconds)
            {
                Static.menuAlertMessage = String.Empty;
                Static.menuAlertColour = 0;
                Static.menuAlertTimer = 0;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            if (Static.menuAlertTimer > 0)
            {
                var pos = new Vector2(512 - (General.Instance.getStringWidth(1, Static.menuAlertMessage) / 2), 50);
                GameRef.SpriteBatch.DrawString(GfxM.FONT_TEXTURE[1], Static.menuAlertMessage, pos, Color.Black);
            }

            base.Draw(gameTime);
        }

        #endregion

        #region Method Region

        public virtual void Transition(ChangeType change, BaseGameState gameState)
        {
            _transitioning = true;
            _changeType = change;
            _transitionTo = gameState;
            _transitionTimer = TimeSpan.Zero;
        }

        #endregion
    }
}
